﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Threading;

namespace DISDisplay
{
    public partial class Form1 : Form
    {
        private Point mouse_offset;
        private bool moving = false;
        Clock clk = null;

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        public static extern IntPtr CreateRoundRectRgn(int nLeftRect, // x-coordinate of upper-left corner            
                                                        int nTopRect, // y-coordinate of upper-left corner            
                                                        int nRightRect, // x-coordinate of lower-right corner            
                                                        int nBottomRect, // y-coordinate of lower-right corner            
                                                        int nWidthEllipse, // height of ellipse            
                                                        int nHeightEllipse // width of ellipse        
                                                         );

        public void DrawRoundRect(Graphics g, Pen p, float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
            gp.CloseFigure();

            g.DrawPath(p, gp);
            gp.Dispose();
        }

        private const int CS_DROPSHADOW = 0x20000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        private Color _Color1 = Color.Green;
        private Color _Color2 = Color.White;
        private float _ColorAngle = 45f;

        public Color Color1
        {
            get { return _Color1; }
            set
            {
                _Color1 = value;
                this.Invalidate(); // Tell the Form to repaint itself

            }
        }

        public Color Color2
        {
            get { return _Color2; }
            set
            {
                _Color2 = value;
                this.Invalidate(); // Tell the Form to repaint itself

            }
        }

        public float ColorAngle
        {
            get { return _ColorAngle; }
            set
            {
                _ColorAngle = value;
                this.Invalidate(); // Tell the Form to repaint itself

            }
        }

        public Form1()
        {
           /* bool wait = true;
            while (wait)
            {
                Process[] pr = Process.GetProcesses();
                foreach (Process p in pr)
                {
                    if (p.ProcessName.Contains("prjCDA"))
                    {
                        Thread.Sleep(1000);
                        wait = false;
                    }
                }
            }*/
           
            InitializeComponent();
            
                Configuration.readXMLParameters();
                Thread.Sleep(Configuration.pause*1000);
                //.Text = DateTime.Now.ToLongTimeString();

                this.Width = Configuration.WindowW;
                this.Height = Configuration.WindowH;
                timer1.Interval = Configuration.RefreshInt * 1000;
                timer1.Enabled = true;
                Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 10, Height - 10, 20, 20));
                
                label1.Visible = false;
                label1.Location = new Point(Configuration.TextCoordX, Configuration.TextCoordY);
                float Fsize = 8.25f;
                try
                {
                    Fsize = float.Parse(Configuration.TextSize.ToString() + ",25");
                }
                catch (Exception ex) { Fsize = float.Parse(Configuration.TextSize.ToString() + ".25"); }
                label1.Font = new System.Drawing.Font("Microsoft Sans Serif", Fsize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(186)));
                clk = new Clock();
                clk.Location = new Point(Configuration.ClockCoordX, Configuration.ClockCoordY);//new Point(this.Width / 2 - 112, this.Height / 2 - 35);
                clk.BringToFront();
                string time = DateTime.Now.ToLongTimeString();
                clk.parseTime(time);
                clk.Visible = true;
                this.Controls.Add(clk);
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (Configuration.isGradientBack)
            {
                _Color1 = Configuration.GradientColor1;
                _Color2 = Configuration.GradientColor2;
                _ColorAngle = Configuration.GradientAngle;
                Graphics g = e.Graphics;
                Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height);
                System.Drawing.Drawing2D.LinearGradientBrush bBackground = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, _Color1, _Color2, _ColorAngle);
                g.FillRectangle(bBackground, rBackground);
                bBackground.Dispose();
            }


            
            if (Configuration.Borders)
            {
                Pen p = new Pen(Color.Black);
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                p.Width = Configuration.WindowBorderW;
                DrawRoundRect(e.Graphics, p, Configuration.WindowBorderW / 2, Configuration.WindowBorderW / 2, Width-(11+Configuration.WindowBorderW) , Height -(11+Configuration.WindowBorderW), 10);
            }
         
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button == System.Windows.Forms.MouseButtons.Left) && (!Configuration.isFixed))
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                this.Location = mousePos; //move the form to the desired location
                moving = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void button1_KeyUp(object sender, KeyEventArgs e)
        {
            if ((e.Control == true) && (e.KeyValue==80))
            {
                Setup_frm sf = new Setup_frm();
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    Configuration.reloadForm(this, timer1,clk,label1);
                }
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control == true) && (e.KeyValue == 80))
            {
                Setup_frm sf = new Setup_frm();
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    Configuration.reloadForm(this, timer1,clk,label1);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Functionality.isConference = Functionality.getMeeting();
            Functionality.renewLabel(label1,timer2,clk);

            
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToLongTimeString();
            clk.parseTime(time);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Configuration.reloadForm(this, timer1,clk,label1);
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (moving)
            {
                toolTip1.Show("X:" + this.Location.X.ToString() + " Y:" + this.Location.Y.ToString(), this);
                moving = false;
            }
        }
    }
}
